local filename = "changephonepassword.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog =require "filelog"
local md5 = require "md5"

local redisdao = require "redisdao"
local json = require "cjson.safe"
json.encode_sparse_array(true, 1, 1)

require "pubenum"

local service = nil
local CMD = {
	service = nil
}

--是否已从redis读取
local is_get_from_redis = false

--错误信息
local userinfo = nil

--
--修改手机密码消息处理
--
function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end

    --验证码为空，尝试从redis读取一次
    local key = rid or service.rid or 0
    if not is_get_from_redis and not service.cur_verif_code then
        local status,data = redisdao.query_data(".gmmqredis", "get", key .. ":cur_verifinfo")
        local cur_verifinfo = json.decode(data) or { }
        if cur_verifinfo then
            service.cur_verif_phonenum = tostring(cur_verifinfo.cur_verif_phonenum)
            service.cur_verif_code = tostring(cur_verifinfo.cur_verif_code)
            service.cur_verif_code_expiratetime = tonumber(cur_verifinfo.cur_verif_code_expiratetime)
        end
        is_get_from_redis = true
    end

     --检测手机号
    if service.info.phonenum == "" or service.info.phonenum ~= msgbody.phonenum then
         return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "错误的手机号"}
    end

    if not service.cur_verif_phonenum or not service.cur_verif_code 
    or service.cur_verif_phonenum ~= msgbody.phonenum then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "错误的验证码"}
    end

    local verif_code = msgbody.verification_code
    local cur_time = timetool.get_time()
    if verif_code == "" or verif_code ~= service.cur_verif_code then
        userinfo = userinfo or {}
        if userinfo.verif_code ~= service.cur_verif_code then
            userinfo.failed_num = nil
            userinfo.start_time = nil
            userinfo.verif_code = service.cur_verif_code
            userinfo.expiratetime = service.cur_verif_code_expiratetime
        end
        userinfo.failed_num = (userinfo.failed_num or 0) + 1
        userinfo.start_time = userinfo.start_time or cur_time

        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "错误的验证码"}
    end

    if cur_time > service.cur_verif_code_expiratetime then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "验证码已过期,请重新获取"}
    end

    --1个验证码最多尝试5次
    if userinfo and userinfo.failed_num >= 5 and userinfo.expiratetime == service.cur_verif_code_expiratetime then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "验证码错误次数过多，请重新获取"}
    end

    --检测密码格式
    local cpass = msgbody.cpass or ""
    local len = string.len(cpass) or 0
    if len < 6 or len > 32 then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "密码格式错误"}
    end

    --更新rs_user表
    local updatelist = { phonecpass = cpass, phonesaltpass = md5.sumhexa(msgbody.phonenum .. cpass)}
    local result = cluster.call("datanode", ".router", "update_rk", rid, "rs_user", nil, updatelist)
    if not result or result[1] == 0 then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "修改密码失败"}
    end

    service.cur_verif_phonenum = nil
    service.cur_verif_code = nil
    service.cur_verif_code_expiratetime = nil
    userinfo = nil

    --删除redis记录
    redisdao.query_data(".gmmqredis", "del", key .. ":cur_verifinfo")

	return { }
end

return CMD